package com.zhangjinghao.controller;

import com.zhangjinghao.entity.Desire;
import com.zhangjinghao.service.DesireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("desire")
public class DesireConrtroller {

    @Autowired
    private DesireService desireService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 跳转意象页面
     * @return
     */
    @RequestMapping("list")
    public String list(){

        List<Desire> desires = null;
        if (!redisTemplate.hasKey("desireList")){
            desires = desireService.selectList();
            redisTemplate.opsForValue().set("desireList",desires,new Random().nextInt(1000)+1000, TimeUnit.MINUTES);
        }else{
            desires = (List<Desire>) redisTemplate.opsForValue().get("desireList");
        }
        request.setAttribute("list",desires);
        return "desire-list";
    }

    /**
     * 跳转添加
     * @return
     */
    @RequestMapping("toInsert")
    public String toInsert(){
        redisTemplate.opsForValue().set("flag-desire","",new Random().nextInt(1000)+1000, TimeUnit.MINUTES);
        return "desire-insert";
    }

    /**
     * 执行添加
     * @param desire
     * @return
     */
    @RequestMapping("insert")
    @ResponseBody
    public int insert(Desire desire){
        if (!redisTemplate.hasKey("flag-desire")){
            return 0;
        }
        int insert = desireService.insert(desire);
        if (insert > 0){
            redisTemplate.delete("flag-desire");
            kafkaTemplate.send("test","id:"+desire.getId(),desire.toString());
        }
        return insert;
    }

    /**
     * 跳转修改
     * @return
     */
    @RequestMapping("toUpdate")
    public String toUpdate(Integer id){
        request.setAttribute("desire",desireService.selectById(id));
        return "desire-update";
    }

    /**
     * 执行修改
     * @param desire
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public int update(Desire desire){

        int update = desireService.update(desire);
        if (update > 0){
            kafkaTemplate.send("test","id:"+desire.getId(),desire.toString());
        }
        return update;
    }

    /**
     * 执行删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public int delete(Integer id){
        int delete = desireService.delete(id);
        if (delete > 0){
            kafkaTemplate.send("test","id:"+id,"删除");
        }
        return delete;
    }
}
